//
//  ItemPopup.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 10/1/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "ItemPopup.h"
#import "MainGameScene.h"
#import "MagnetZone.h"
#import "UserInfo.h"
#import "ShopScene.h"
#import "ShieldZone.h"
#import "ItemInfo.h"
#import "Utility.h"

@implementation ItemPopup

-(id) initWithType:(ITEM_TYPE)type
{
    if (self = [super initWithColor:ccc4(0, 0, 0, 0)])
    {
        mType = type;
        
        [[MainGameScene getInstance] pause];
        [self initMenu];
        
        self.touchEnabled = YES;
        
        CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:POPUP_ANIMATE_TIME opacity:POPUP_ALPHA];
        [self runAction:fadeTo];
        
        for (int i = 0; i < _children.count; i++)
        {
            CCFadeTo *f = [CCFadeTo actionWithDuration:0.3 opacity:255];
            CCSprite *node = [_children objectAtIndex:i];
            [node setOpacity:0];
            [node runAction:f];
        }
    }
    
    return self;
}

-(void) registerWithTouchDispatcher
{
    [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:TOUCH_PRIORITY_RESULT_POPUP swallowsTouches:YES];
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    return YES;
}

-(void) initMenu
{
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    CCSprite *panel = [CCSprite spriteWithSpriteFrameName:SFN_PANEL_1];
    [panel setPosition:ccp(winSize.width / 2, winSize.height / 2 + uniPos(50, 100))];
    [panel setColor:CL_PANEL_BG];
    [self addChild:panel];
    
    
    // create menu
    mMenu = [[CCMenu alloc] init];
    [mMenu setPosition:ccp(0, 0)];
    [self addChild:mMenu];
    
    // create button main menu
    CCSprite *sprMainMenu = [CCSprite spriteWithSpriteFrameName:SFN_PANEL_BTN_1_2];
    CCSprite *sprMainMenuHover = [CCSprite spriteWithSpriteFrameName:SFN_PANEL_BTN_1_2];
    [sprMainMenu setColor:CL_PANEL_BG];
    [sprMainMenuHover setColor:CL_PANEL_BTN];
    CCMenuItem *miMainMenu = [CCMenuItemSprite itemWithNormalSprite:sprMainMenu
                                                     selectedSprite:sprMainMenuHover
                                                             target:self
                                                           selector:@selector(onBack)];
    [miMainMenu setPosition:ccp(winSize.width / 2 - uniPos(70.5, 141), panel.position.y - uniPos(101, 202))];
    [mMenu addChild:miMainMenu];
    
    
    // create button replay
    CCSprite *sprReplay = [CCSprite spriteWithSpriteFrameName:SFN_PANEL_BTN_1_2];
    CCSprite *sprReplayHover = [CCSprite spriteWithSpriteFrameName:SFN_PANEL_BTN_1_2];
    [sprReplay setColor:CL_PANEL_BG];
    [sprReplayHover setColor:CL_PANEL_BTN];
    [sprReplay setFlipX:YES];
    [sprReplayHover setFlipX:YES];
    CCMenuItem *miReplay = [CCMenuItemSprite itemWithNormalSprite:sprReplay
                                                   selectedSprite:sprReplayHover
                                                           target:self
                                                         selector:@selector(onAccept)];
    [miReplay setPosition:ccp(winSize.width / 2 + uniPos(70.5, 141), panel.position.y - uniPos(101, 202))];
    [mMenu addChild:miReplay];
    
    // create back icon
    CCSprite *iconBack = [CCSprite spriteWithSpriteFrameName:SFN_ICON_CANCEL];
    [iconBack setPosition:miMainMenu.position];
    [self addChild:iconBack];
    
    CCSprite *iconAccept = [CCSprite spriteWithSpriteFrameName:SFN_ICON_SHOP];
    [iconAccept setPosition:miReplay.position];
    [self addChild:iconAccept];
    
    ItemIcon *ii = [[MainGameScene getInstance]._mainGameUI getItemIconWithType:mType];
    if ([[UserInfo getInstance] getNumItem:mType] > 0 && ![ii isUseMaxItem])
    {
        [iconAccept setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:SFN_ICON_CHECK]];
    }
    
    CCSprite *sprIndicator = [[ItemInfo getInstance] getItemSprite:mType];
    [sprIndicator setPosition:ccp(panel.position.x, panel.position.y + uniPos(30, 60))];
    [self addChild:sprIndicator];
    
    CCLabelBMFont *lbName = [CCLabelBMFont labelWithString:@"" fntFile:FONT_DEFAULT];
    [lbName setString:[[ItemInfo getInstance] getItemName:mType]];
    [lbName setAnchorPoint:ccp(0.5, 0.5)];
    [lbName setPosition:ccp(sprIndicator.position.x, uniPos(sprIndicator.position.y - 40, sprIndicator.position.y - 80))];
    [self addChild:lbName];
    
    CCLabelBMFont *lbDesc = [CCLabelBMFont labelWithString:@"" fntFile:FONT_DEFAULT];
    [lbDesc setString:[[ItemInfo getInstance] getItemDesc:mType]];
    [lbDesc setAnchorPoint:ccp(0.5, 0.5)];
    [lbDesc setPosition:ccp(sprIndicator.position.x, uniPos(sprIndicator.position.y - 80, sprIndicator.position.y - 160))];
    [self addChild:lbDesc];
    
    [mMenu setTouchPriority:TOUCH_PRIORITY_RESULT_POPUP];
}

-(void) onBack
{
    [[Utility getInstance] playEffect:SOUND_MENU_BUTTON];
    CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:POPUP_ANIMATE_TIME opacity:0];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self removeFromParent];
        [[MainGameScene getInstance] resume];
    }];
    CCSequence *sequence = [CCSequence actions:fadeTo, callBlock, nil];
    [self runAction:sequence];
    
    for (int i = 0; i < _children.count; i++)
    {
        CCFadeTo *f = [CCFadeTo actionWithDuration:0.3 opacity:0];
        CCSprite *node = [_children objectAtIndex:i];
        [node runAction:f];
    }
}

-(void) onAccept
{
    [[Utility getInstance] playEffect:SOUND_MENU_BUTTON];
    ItemIcon *ii = [[MainGameScene getInstance]._mainGameUI getItemIconWithType:mType];
    if ([[UserInfo getInstance] getNumItem:mType] == 0 || [ii isUseMaxItem])
    {
        [[CCDirector sharedDirector] pushScene:[ShopScene scene]];
    }
    else
    {
        [self onUseItem];
        int currentNum = [[UserInfo getInstance] getNumItem:mType];
        [[UserInfo getInstance] setNumItem:mType num:(currentNum - 1)];
        [[UserInfo getInstance] saveUserInfo];
        [[MainGameScene getInstance]._mainGameUI updateItemInfo];
        [[MainGameScene getInstance] resume];
    }
    [self removeFromParent];
}

-(void) onUseItem
{
    
    if (mType == eIT_SHIELD)
    {
        [[Utility getInstance] playEffect:SOUND_SHIELD];
        [LevelManager getInstance]._itemShieldTime = 0;
        
        if ([[MainGameScene getInstance] getChildByTag:TAG_SHIELD_ZONE] != nil)
        {
            [[MainGameScene getInstance] removeChildByTag:TAG_SHIELD_ZONE];
        }


        PNode *userNode = [[MainGameScene getInstance] getUserNode];
        ShieldZone *shield = [[ShieldZone alloc] initWithUserNode:userNode];
        [[MainGameScene getInstance] addChild:shield z:0 tag:TAG_SHIELD_ZONE]; // need to update schedule
        [shield addToBatch:[MainGameScene getInstance]._globalBatch];
        [shield release];
        
        ItemIcon *ii = [MainGameScene getInstance]._mainGameUI._miItemGlove;
        [ii setNumItemUse:1];
    }
    else if (mType == eIT_XRAY)
    {
        [[Utility getInstance] playEffect:SOUND_SUN];
        [[MainGameScene getInstance]._darkFog zoomOut];
        ItemIcon *ii = [MainGameScene getInstance]._mainGameUI._miItemXray;
        [ii setNumItemUse:1];
    }
    else if (mType == eIT_TIMER)
    {
        [[Utility getInstance] playEffect:SOUND_TIME];
        [MainGameScene getInstance]._mainGameUI._levelTimeShow = [LevelManager getInstance]._levelTime;
        [LevelManager getInstance]._levelTime += ITEM_TIMER_PLUS;
        [[MainGameScene getInstance]._mainGameUI updateTimeNextTime];
        ItemIcon *ii = [MainGameScene getInstance]._mainGameUI._miItemTimer;
        [ii setNumItemUse:[ii getNumItemUse] + 1];
    }
    else if (mType == eIT_MAGNET)
    {
//        [LevelManager getInstance]._itemMagnetTime = 10;
        [[Utility getInstance] playEffect:SOUND_MAGNET];
        PNode *userNode = [[MainGameScene getInstance] getUserNode];
        MagnetZone *magnetZone = [[MagnetZone alloc] initWithUserNode:userNode];
        [[MainGameScene getInstance]._globalBatch addChild:magnetZone z:Z_ORDER_MAGNET_ZONE];
        [magnetZone release];
        
        ItemIcon *ii = [MainGameScene getInstance]._mainGameUI._miItemMagnet;
        [ii setNumItemUse:1];
    }
}

-(void) dealloc
{
    [super dealloc];
    CCLOG(@"Item popup dealloc!");
}

@end
